﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Webcar.BLL;
using BusinessEntity;
using ToolsLayer;
using System.IO;

namespace Webcar.SysManage
{
    public partial class AddFiles : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack) return;
            this.lblDateTime.Text = DateTime.Now.ToString();
            this.lblMan.Text = CurrentUser.GetLoginUserName();
            string sql = "select * from BS_CODE where BASE_KIND='I'";

            ToolsLayer.PublicFunction.BindList(this.ddlFileKind, sql, "base_code", "base_name");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.ddlFileKind.SelectedValue == "")
            {
                this.Alert("请选择分类。");
                return;
            }
            if (string.IsNullOrEmpty(this.FileUpload1.FileName))
            {
                this.Alert("请选择文件。");
                return;
            }
            string ext = "rar|zip|doc|xls|ppt|docx|jpg|jpeg";
            string fileext = Path.GetExtension(this.FileUpload1.FileName);
            if (ext.IndexOf(fileext.Substring(fileext.LastIndexOf(".") + 1)) < 0)
            {
                this.Alert("该格式的文件不能上传，请压缩。");
                return;
            }
            FileUp fileup = new FileUp(@"/webcar/files/", ext);
            fileup.FilePath = Server.MapPath("/webcar/files/") + Path.GetFileName(this.FileUpload1.FileName);
            fileup.fileSaveAs(this.FileUpload1.PostedFile);
            BS_FILESEntity files = new BS_FILESEntity();
            files.FileDate = DateTime.Now;
            files.FileName = this.FileUpload1.FileName;
            files.FileLen = this.FileUpload1.FileBytes.Length;
            files.Kind = this.ddlFileKind.SelectedValue;
            files.OperCode = CurrentUser.LoginUserID;
            files.Description = this.txtDescription.Text;
            files.Save();
            Response.Redirect("~/SysManage/Files.aspx");
        }
    }
}
